import pygame

pygame.init()
screen = pygame.display.set_mode((750,850))
pygame.display.set_caption("FIVE_IN_A_ROW")

border_left = 25
border_right = 725
border_top = 25
border_bottom = 725
width = 50
height = 50

running = True

font = pygame.font.Font('font.ttf', 24)

class Button:
    def __init__(self,x,y,width,height,text,color,click_color,text_color) -> None:
        self.text = text
        self.color = color
        self.click_color = click_color
        self.text_color = text_color
        self.rect = pygame.Rect(x,y,width,height)
        self.clicked = False

    def draw(self,screen):
        if self.clicked:
            pygame.draw.rect(screen,self.click_color,self.rect)
        else:
            pygame.draw.rect(screen,self.color,self.rect)

        text_surface = font.render(self.text,True,self.text_color)
        text_rect = text_surface.get_rect(center = self.rect.center)
        screen.blit(text_surface,text_rect)

class Game:
    def __init__(self) -> None:
        self.player = 1
        self.winner = 0
        map = [0] * 15
        for i in range(15):
            self.map[i] = [0]*15
    def start(self) -> None:
        screen.fill("#EE9A49")

        # 竖线
        for x in range(15):
            pygame.draw.line(screen, "#000000", [border_left + width * x, border_top],[border_left + width * x, border_bottom], 2)
        # 横线
        for y in range(15):
            pygame.draw.line(screen, "#000000", [border_left, border_top + height * y],[border_right, border_top + height * y], 2)

        pygame.draw.circle(screen, "#000000", [25 + 50 * 7, 25 + 50 * 7], 8)

        x, y = pygame.mouse.get_pos()
        if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom:
            x = round((x - border_left) / width) * width + border_left
            y = round((y - border_top) / height) * height + border_top

            pygame.draw.rect(screen, "#FFFFFF", [x - 25, y - 25, 50, 50], 2)

        button.draw(screen)
        button_ai.draw(screen)

        for row in range(15):
            for col in range(15):
                if map[row][col] == 1:
                    pygame.draw.circle(screen, "#000000", [col * width + border_left, row * height + border_top], 25)
                elif map[row][col] == 2:
                    pygame.draw.circle(screen, "#FFFFFF", [col * width + border_left, row * height + border_top], 25)

        if (winner != 0):
            if winner == 1:
                text = 'black player win!'
                color = (0, 0, 0)
            else:
                text = 'white player win!'
                color = (255, 255, 255)
            font = pygame.font.Font('font.ttf', 70)
            text_surface = font.render(text, True, color)
            text_position = (100, 100)
            screen.blit(text_surface, text_position)
            pygame.display.update()
            pygame.time.wait(3000)
            running = False

    def check(slef,row,col):
    #左右是否连线
    score = 1
    for i in range(4):
        try:
            if slef.map[row][col+i] == slef.map[row][col+i+1]:
                score = score + 1
            else:
                break
        except:
            break
    for i in range(4):
        try:
            if slef.map[row][col-i] == slef.map[row][col-i-1]:
                score = score + 1
            else:
                break
        except:
            break
    if score >= 5:
        return True

    #上下是否连线
    score = 1
    for i in range(4):
        try:
            if slef.map[row+i][col] == slef.map[row+i+1][col]:
                score = score + 1
            else:
                break
        except:
            break
    for i in range(4):
        if slef.map[row-i][col] == slef.map[row-i-1][col]:
            score = score + 1
        else:
            break
    if score >= 5:
        return True

    # 斜边是否连线
    score = 1
    for i in range(4):
        try:
            if slef.map[row+i][col+i] == slef.map[row+i+1][col+i+1]:
                score = score + 1
            else:
                break
        except:
            break
    for i in range(4):
        try:
            if slef.map[row-i][col-i] == slef.map[row-i-1][col-i-1]:
                score = score + 1
            else:
                break
        except:
            break
    if score >= 5:
        return True


    score = 1
    for i in range(4):
        try:
            if slef.map[row-i][col+i] == slef.map[row-i-1][col+i+1]:
                score = score + 1
            else:
                break
        except:
            break
    for i in range(4):
        try:
            if slef.map[row+i][col-i] == slef.map[row+i+1][col-i-1]:
                score = score + 1
            else:
                break
        except:
            break
    if score >= 5:
        return True

button = Button(50, 750, 100, 50, "双人模式", (153, 51, 250), (221, 160, 221), (255, 255, 255))
button_ai = Button(200, 750, 100, 50, "AI模式", (153, 51, 250), (221, 160, 221), (255, 255, 255))
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            if x >= border_left and x <= border_right and y >= border_top and y <= border_bottom:
                col = round((x - 25)/50)
                row = round((y - 25)/50)
                if map[row][col] == 0:
                    print(row+1,col+1)
                    map[row][col] = player
                    if(check(row,col)):
                        winner = player
                    else:
                        if player == 1:
                            player = 2
                        else:
                            player = 1
                else:
                    print("你还没看到有棋子了吗？")


        pygame.display.update()

pygame.quit()